
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">订单编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="order_sn" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="goods_title" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">支付方式</label>
                        <div class="layui-input-block">
                            <div name="pay_type" id="pay_type" value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">核销状态</label>
                        <div class="layui-input-block">
                            <div name="is_verification" id="is_verification" value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">退款状态</label>
                        <div class="layui-input-block">
                            <div name="is_refund" id="is_refund" value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">订单状态</label>
                        <div class="layui-input-block">
                            <div name="order_status" id="order_status" value="" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="details" permission="app.admin.rlorder.details">详情</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.rlorder.delete">删除</button>
            {{# if(d.order_status==1 && d.is_refund==1){}}
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="refund" permission="app.admin.rlorder.refund">退款</button>
            {{# } }}
        </script>

        <script type="text/html" id="createTime">
            {{# if(d.create_time>0){}}
            {{layui.util.toDateString(d.create_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script type="text/html" id="refundTime">
            {{# if(d.refund_time>0){}}
            {{layui.util.toDateString(d.refund_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script type="text/html" id="payTime">
            {{# if(d.pay_time>0){}}
            {{layui.util.toDateString(d.pay_time+'000', 'yyyy-MM-dd HH:mm:ss')}}
            {{# } }}
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/rl-order/select";
            const DELETE_API = "/app/admin/rl-order/delete";
            const DETAILS_URL = "/app/admin/rl-order/details";
            const REFUND_API ="/app/admin/rl-order/refund";

            // 字段 支付方式，1微信支付，2支付宝 pay_type
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#pay_type").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#pay_type",
                    name: "pay_type",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"微信支付"},{"value":"0","name":"支付宝"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                })
            });
            
            // 字段 核销状态，1已核销，2未核销 is_verification
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#is_verification").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#is_verification",
                    name: "is_verification",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"已核销"},{"value":"0","name":"未核销"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                })
            });
            
            // 字段 退款状态，1未启用，2退款中，3已退款 is_refund
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#is_refund").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#is_refund",
                    name: "is_refund",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"未启用"},{"value":"2","name":"退款中"},{"value":"3","name":"已退款"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                })
            });
            
            // 字段 订单状态，0待支付，1已支付，2超时已取消，3手动取消，8已完成，9已退款 order_status
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#order_status").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#order_status",
                    name: "order_status",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"0","name":"待支付"},{"value":"1","name":"已支付"},{"value":"2","name":"超时已取消"},{"value":"3","name":"手动取消"},{"value":"8","name":"已完成"},{"value":"9","name":"已退款"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                })
            });
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						title: "订单ID",align: "left",
						field: "id",
                        width: 100,
					},{
						title: "订单编号",align: "left",
						field: "order_sn",
                        width: 150,
					},{
                        title: "用户ID",align: "left",
                        field: "member_id",
                        width: '100',
                        hide:true,
                    },{
                        title: "购买人昵称",align: "left",
                        field: "nickname",
                        width: 120,
                    },{
                        title: "购买人头像",align: "left",
                        field: "avatar",
                        width: 120,
                        templet: function (d) {
                            return '<img src="'+encodeURI(d['avatar'])+'" style="max-width:32px;max-height:32px;" alt="" />'
                        }
                    },{
						title: "商品ID",align: "left",
						field: "goods_id",
                        width: 80,
                        hide:true,
					},{
                        title: "商品图片",align: "left",
                        field: "goods_icon",
                        width: 120,
                        templet: function (d) {
                            return '<img src="'+encodeURI(d['goods_icon'])+'" style="max-width:32px;max-height:32px;" alt="" />'
                        }
                    },{
						title: "营期名称",align: "left",
						field: "goods_period_title",
                        width: 120,
					},{
                        title: "商品标题",align: "left",
                        field: "goods_title",
                        width: 150,
                    },{
						title: "购买数量",align: "left",
						field: "goods_num",
                        width: 100,
					},{
						title: "订单金额",align: "left",
						field: "order_amount",
                        width: 120,
					},{
						title: "实际支付金额",align: "left",
						field: "order_actual_amount",
                        width: 120,
					},{
						title: "交易流水号",align: "left",
						field: "order_trade_no",
                        width: 150,
                        hide:true,
					},{
						title: "订单状态",align: "left",
						field: "order_status",
						templet: function (d) {
                            let css = {"0":"layui-bg-warn","1":"layui-bg-blue","2":"layui-bg-green","3":"layui-bg-green","8":"layui-bg-blue","9":"layui-bg-red"}[d["order_status"]];
                            let title = {"0":"待支付","1":"已支付","2":"超时已取消","3":"手动取消","8":"已完成","9":"已退款"}[d["order_status"]];
                            return '<span class="layui-badge '+css+'">'+util.escape(title)+'</span>';
						},
                        width: 120,
					},{
                        title: "订单备注",align: "left",
                        field: "order_remark",
                        hide: true,
                        width: 120,
                    },{
                        title: "支付方式",align: "left",
                        field: "pay_type",
                        templet: function (d) {
                            let field = "pay_type";
                            if (typeof d[field] == "undefined") return "";
                            let payTypes={"1":"微信支付","2":"支付宝支付"};
                            return util.escape(payTypes[d[field]]);
                        },
                        width: 120,
                    },{
						title: "创建时间",align: "left",
						field: "create_time",
                        templet: '#createTime',
                        width: 120,
					},{
                        title: "支付时间",align: "left",
                        field: "pay_time",
                        templet: '#payTime',
                        width: 120,
                    },{
                        title: "退款时间",align: "left",
                        field: "refund_time",
                        templet: '#refundTime',
                        width: 120,
                    },{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 200,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				// 获取表格中下拉或树形组件数据
				let apiResults = {};
				apiResults["pay_type"] = [];
				apiResults["order_status"] = [];
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "refund") {
                        refund(obj);
                    }else if (obj.event==='details'){
                        details(obj)
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let details = function() {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "订单详情",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: DETAILS_URL+ "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                //退款操作
                let refund=function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "订单退款",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"300px"],
                        content: REFUND_API+ "?" + PRIMARY_KEY + "=" + value
                    });
                }

                //执行退款
                let doRefund = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定退款?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: REFUND_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

        </script>
    </body>
</html>
